package com.wealth.member.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.bank.entity.Picked;
import com.wealth.bank.service.IPickedService;
import com.wealth.cart.entity.Cart;
import com.wealth.cart.entity.CartGoodsInfo;
import com.wealth.cart.entity.CartItem;
import com.wealth.cart.enums.CartStatusEnum;
import com.wealth.cart.service.ICartService;
import com.wealth.coupon.entity.CouponRecord;
import com.wealth.coupon.service.ICouponRecordService;
import com.wealth.member.entity.Address;
import com.wealth.member.entity.Member;
import com.wealth.member.service.IAddressService;
import com.wealth.member.service.IMemberService;
import com.wealth.merchant.entity.BusinessDistrict;
import com.wealth.merchant.service.IBusinessDistrictService;
import com.wealth.order.service.IOrderPlatService;
import com.wealth.order.vo.CommodityOrderVO;
import com.wealth.platform.entity.PlatformActivity;
import com.wealth.platform.service.IPlatformActivityService;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import iaas.utils.ResultObject;

/**
 * 实体商品订单
 * @author Administrator
 */

@Controller
@RequestMapping(value="/manage/commodityOrder")
public class GoodsOrderController extends BaseController{
	
	Logger logger = Logger.getLogger(GoodsOrderController.class);
	@Autowired
	private ICartService cartService;
	@Autowired
	private ICouponRecordService recordService;
	@Autowired
	private IMemberService memberService;
	@Autowired
	private IAddressService addressService;
	@Autowired
	private IOrderPlatService orderPlatService;
	@Autowired
	private IPlatformActivityService activityService;
	@Autowired
	private IBusinessDistrictService businessDistrictService;
	@Autowired
	private IPickedService pickedService;
	//订单页面展示
	@RequestMapping(value = "/order/{marketId}",method=RequestMethod.GET)
	public String index(HttpServletRequest request, Model model,@PathVariable Long marketId) {
		try {
			//封装商户和商品
			List<CartGoodsInfo> cartGoodsList  = cartService.getCheckedCar(memberid,marketId);
			model.addAttribute("cartGoodsList", cartGoodsList);
			
			
			List<CartGoodsInfo> cart_list=new ArrayList<CartGoodsInfo>();//普通商户
			List<CartGoodsInfo> cartPicked_list=new ArrayList<CartGoodsInfo>();//自提商户
			
			//区分商品种类
			for (int i = 0; i < cartGoodsList.size(); i++) {
				System.out.println();
				Integer isPicked=cartGoodsList.get(i).getMerchant().getIsPicked();
				if(isPicked==null || isPicked==0 ){
					cart_list.add(cartGoodsList.get(i));
				}else {
					cartPicked_list.add(cartGoodsList.get(i));
				}
			}
			//父商圈查询(用于自提点选择)
			if(cartPicked_list.size()>0){
				List<BusinessDistrict> businessDistrictList = businessDistrictService.getByParentIdIsNullAndStatus(StatusEnum.NORMAL.getValue());
				model.addAttribute("businessDistrictList", businessDistrictList);
			}
			model.addAttribute("cart_list", cart_list);//普通商户
			model.addAttribute("cartPicked_list",cartPicked_list);//自提商户
			
			Cart cart=cartService.findCartByMemeberIdAndMarketId(memberid, marketId);
			//获取购物车中选中商品总金额
			double total=cartService.getTotal(cart);
			//查询平台活动
			List<PlatformActivity> bankActivities=activityService.findByStatusAndType(StatusEnum.NORMAL.getValue(),0);
			if(bankActivities.size()>0){
				//够
				if(total>=bankActivities.get(0).getLimitmoney()){
					
					total=total-bankActivities.get(0).getCutmoney();//减免后总金额
					model.addAttribute("activity", "减免金额为:"+bankActivities.get(0).getCutmoney()+"元");
				}else{
				//不够
					double sub=bankActivities.get(0).getLimitmoney()-total;
					model.addAttribute("activity", "你还差"+sub+"元够满减");
				}
			}
			
			model.addAttribute("total", total);
			
			//会员可用优惠券
			List<CouponRecord> couponRecords=recordService.getUsableCouponRecord(memberid, total);
			model.addAttribute("couponRecords", couponRecords);
			
			//会员绑定的手机号
			Member member= memberService.get(memberid);
			model.addAttribute("phone", member.getUsername());
			
			//会员默认选择的地址
			Address address=addressService.getAddressDefault(memberid);
			model.addAttribute("memberAddress", address);
			model.addAttribute("marketId", marketId);
			model.addAttribute("flag", "mycomment");
			return "order/order";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return "system/500";
		}
	}
	

	/** 
	 * 创建订单
	 */
	@RequestMapping(value="/create",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject create(
			@RequestParam Long marketid,
			@RequestParam Long addressid ,
			@RequestParam Long couponRecordId ,
			@RequestParam Integer paymode ) {
		try{
			return orderPlatService.saveOrder(corpid, memberid,marketid,addressid,couponRecordId,paymode, couponRecordId);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultObject("999", "创建订单错误", "");
		}
	}
	
	
	/**
	 * 查询商圈子分类
	 */
	@RequestMapping(value="/getDistrict",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject getDistrict(HttpServletRequest request,Long  districtId){
		try{
			List<BusinessDistrict> list=businessDistrictService.getByParentIdAndStatus(districtId, StatusEnum.NORMAL.getValue());
			return new ResultObject("000", "查询商圈成功", list);
		}catch(Exception e){
			logger.error("查询商圈错误", e);
			return new ResultObject("999", "查询商圈错误", "");
		}
	}
	
	/**
	 * 查询自提点
	 */
	@RequestMapping(value="/getPicked",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject getPicked(HttpServletRequest request,Long  districtId){
		try{
			List<Picked> list=pickedService.findByStatusAndDistrictId(StatusEnum.NORMAL.getValue(), districtId);
			return new ResultObject("000", "查询自提点成功", list);
		}catch(Exception e){
			logger.error("查询自提点错误", e);
			return new ResultObject("999", "查询自提点错误", "");
		}
	}
	
	
}
